/*
 * Dir Poller to see if we have a new video after we send the video 
 * we should remove it and continue polling !
 */

package tcpserver;

import java.io.*;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author makkalot aka ninja coder :) 
 */
public class DirPoller {
    
    private LinkedList<String> changedFiles;
 //   public static final String dir_to_scan = "/home/makkalot/dontmove/dontmove/dontmove/motion_man/notrecognized"; //change it for your needs
    public static final String dir_to_scan = "c:\\Documents and Settings\\Semih Altınsoy\\Belgelerim\\NetBeansProjects\\trunk\\motion_man\\notrecognized"; //change it for your needs
    private File folder;
    
    public DirPoller() throws IOException{
    
        this.folder = new File(DirPoller.dir_to_scan);
        if (!this.folder.exists()){
            System.out.println();
            try {
                throw new Exception("The file doesnt exists :" + DirPoller.dir_to_scan);
            } catch (Exception ex) {
                Logger.getLogger(DirPoller.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.changedFiles = new LinkedList<String>();
    }
    
    public boolean isThereNewFile(){
    
        int count = 0;
        
        File[] fileList = this.folder.listFiles();
        
        this.changedFiles.clear();
        
        for(int i=0;i<fileList.length;i++){
            
            
            if(fileList[i].isFile()){
                String[] temp_list = null;
                temp_list = fileList[i].getName().split("\\.");
                
                
                if(temp_list.length>1 &&temp_list[temp_list.length-1].equals("avi")){
                    count ++;
                    System.out.println(fileList[i].getName());
                    this.changedFiles.add(fileList[i].getName());
                }
            }
        } 
        
        
        if(count!=0)
            return true;
        
        return false;
        
    }
    
    public LinkedList<String> getChangedFileName(){
        return this.changedFiles;
    }
    
    public void sendViaClient(){
    
        System.out.println("The list im sending is :");
        try{
        for(int i=0; i<this.changedFiles.size();i++){
            System.out.println(this.changedFiles.get(i));
            //cleanup the files
            
            //sending
            System.out.println("Sending ...");
            TestClient t = new TestClient();
            t.sendData(DirPoller.dir_to_scan + File.separatorChar + this.changedFiles.get(i));
            //t=null;
            
            File n = new File(DirPoller.dir_to_scan + File.separatorChar + this.changedFiles.get(i));
            if(!n.delete()){
                System.out.println("File couldnt be deleted :"+n.getName());
            }
        }//end of for
        }//end of try
        catch(Exception e){
        
            System.err.println("Error in sending changed data ");
        }
    }
    
    public static void main(String[] args){
        
        //testing the poller class 
        
        try{
          DirPoller dp = new DirPoller();
          int waitme = 20000; //20 sec is good
          
          while(true){
          
                if(dp.isThereNewFile()){
                    System.out.println("New files send it");
                    dp.sendViaClient();
                }
                else{
                
                    System.out.println("No change in directory !");
                }
          
            System.out.println("Lets sleep a little bit :) :" + waitme);
            Thread.sleep(waitme);
          }
        }
        catch (InterruptedException ex) {
            Logger.getLogger(DirPoller.class.getName()).log(Level.SEVERE, null, ex);
        }        catch(IOException e){
            System.out.println("Error in polling");
        }
    }
}
